class Solution
{
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    bool check[51][51];

public:
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        int ret = 0;
        int m = grid.size(), n = grid[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (grid[i][j] == 1 && !check[i][j])
                    ret = max(ret, dfs(grid, i, j));
            }
        }
        return ret;
    }
    int dfs(vector<vector<int>>& grid, int i, int j)
    {
        int count = 0;
        queue<pair<int, int>> q;
        q.push({ i,j });
        check[i][j] = true;
        count++;
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1 && check[x][y] == false)
                {
                    q.push({ x,y });
                    count++;
                    check[x][y] = true;
                }
            }
        }
        return count;
    }
};